package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.system.Security;
	
	import br.com.stimuli.loading.loadingtypes.LoadingItem;
	
	import communication.ServerAddress;
	import communication.hall.HallSocketManager;
	import communication.login.LoginSocketManager;
	
	import modules.GameDispatcher;
	import modules.load.Load;
	import modules.load.LoadEvent;
	import modules.lobby.Lobby;
	import modules.login.Login;
	import modules.login.LoginEvent;
	
	import utils.ConversionTable;
	import utils.RequestController;

	[SWF(width = "1000", height = "650")]
	public class Majiang extends Sprite
	{
		private var dispatcher:GameDispatcher = GameDispatcher.instance;

		private var requestController:RequestController;

		private var requestController1:RequestController;

		public function Majiang()
		{
			Security.allowDomain("*");

			init();
		}

		private function init():void
		{
			MyCC.initFlashConsole(this);

			initUI();
			initMoudles();

			loadConfig();

			dispatcher.dispatchEvent(new LoginEvent(LoginEvent.LOGIN_SHOW));
		}

		/**
		 * 初始化模块
		 **/
		private function initMoudles():void
		{
			Load.init();
			Login.init();
			Lobby.init();

			LoginSocketManager.init();
			HallSocketManager.init();
		}

		private function initUI():void
		{
			UIAllRefer.stage = this.stage;

			this.stage.addChild(UIAllRefer.backLayer);
			this.stage.addChild(UIAllRefer.contentLayer);
			this.stage.addChild(UIAllRefer.popLayer);
			this.stage.addChild(UIAllRefer.infoLayer);
			this.stage.addChild(UIAllRefer.tipLayer);
		}

		private function loadConfig():void
		{
			dispatcher.dispatchEvent(new LoadEvent(LoadEvent.LOAD_RESOURCE, {urls: [GlobalData.configPath]}));

			dispatcher.addEventListener(LoadEvent.LOAD_SINGLE_COMPLETE, onLoadSingleComplete);
		}

		private function onLoadSingleComplete(event:LoadEvent):void
		{
			var loadingItem:LoadingItem = event.data;
			if (GlobalData.configPath == loadingItem.url.url)
			{
				dispatcher.removeEventListener(LoadEvent.LOAD_SINGLE_COMPLETE, onLoadSingleComplete);
				var config:XML = Load.loader.getXML(GlobalData.configPath);

				config.ignoreWhitespace = true;

				//初始化登陆服务器列表
				var server:ServerAddress = new ServerAddress();
				server.host = config.server.host;
				server.port = config.server.port;
				server.policy = config.server.policy;
				GlobalData.loginServerArray.push(server);

				GlobalData.policy = config.server.policy;

				GlobalData.httpServerHost = config.httpServer.host;
				Security.loadPolicyFile("http://" + GlobalData.httpServerHost + "/crossdomain.xml");
				requestCoin();
				requestVPP();
			}
		}

		private function requestCoin():void
		{
			requestController = new RequestController();
			requestController.request(URLRequestMethod.GET, "http://" + GlobalData.httpServerHost + "/Manager/Module/Controls/ClientInterFace/Coin.aspx", new URLVariables());

			requestController.addEventListener(Event.COMPLETE, onHttpComplete);
			requestController.addEventListener(IOErrorEvent.IO_ERROR, function(event:Event):void
			{
				logger("加载汇率表失败");
				requestController.request(URLRequestMethod.GET, "http://" + GlobalData.httpServerHost + "/Manager/Module/Controls/ClientInterFace/Coin.aspx", new URLVariables());
			});
		}

		private function requestVPP():void
		{
			requestController1 = new RequestController();
			requestController1.request(URLRequestMethod.GET, "http://" + GlobalData.httpServerHost + "/Manager/Module/Controls/ClientInterFace/Vpp.aspx", new URLVariables());

			requestController1.addEventListener(Event.COMPLETE, onHttpComplete1);
			requestController1.addEventListener(IOErrorEvent.IO_ERROR, function(event:Event):void
			{
				logger("加载vpp表失败");
				requestController1.request(URLRequestMethod.GET, "http://" + GlobalData.httpServerHost + "/Manager/Module/Controls/ClientInterFace/Vpp.aspx", new URLVariables());
			});
		}

		private function onHttpComplete(event:Event):void
		{
			logger("获取汇率表");
			var config:XML = null;

			try
			{
				config = XML(requestController.data);
			}
			catch (error:Error)
			{
				config = null;
				logger("汇率表 解析错误：\n" + requestController.data.toString());
			}

			//初始化 金币直接的转化类
			config && ConversionTable.init(config);
		}

		private function onHttpComplete1(event:Event):void
		{
			logger("获取vpp表");

			var config:XML = null;

			try
			{
				config = XML(event.currentTarget.data);

				var vppConfigData:Array = GlobalData.vppConfigData;
				for (var i:int = 0; i < 5; i++)
				{
					if (config.item[i])
					{
						vppConfigData[i] = int(config.item[i].@exp);
					}
				}

			}
			catch (error:Error)
			{
				config = null;
				logger("vpp表 解析错误：\n" + requestController.data.toString());
			}

		}

	}
}
